/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChooseOntoFrame.java
 *
 * Created on 18/08/2010, 13:07:11
 */

package gui;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import matching.xml.MatchingManager;
import org.semanticweb.owl.model.OWLOntologyCreationException;

/**
 *
 * @author livia
 */
public class ChooseOntoFrame extends javax.swing.JFrame {

    /** Creates new form ChooseOntoFrame */
    public ChooseOntoFrame() {
        initComponents();
        setResizable(false);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldSource = new javax.swing.JTextField();
        jTextFieldTarget = new javax.swing.JTextField();
        jButtonSource = new javax.swing.JButton();
        jButtonTarget = new javax.swing.JButton();
        jButtonOk = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("uri ontology source");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText("uri ontology target");
        jLabel2.setName("jLabel2"); // NOI18N

        jTextFieldSource.setName("jTextFieldSource"); // NOI18N

        jTextFieldTarget.setName("jTextFieldTarget"); // NOI18N

        jButtonSource.setText("choose");
        jButtonSource.setName("jButtonSource"); // NOI18N
        jButtonSource.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSourceActionPerformed(evt);
            }
        });

        jButtonTarget.setText("choose");
        jButtonTarget.setName("jButtonTarget"); // NOI18N
        jButtonTarget.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonTargetActionPerformed(evt);
            }
        });

        jButtonOk.setText("ok");
        jButtonOk.setName("jButtonOk"); // NOI18N
        jButtonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOkActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTextFieldTarget, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
                    .addComponent(jTextFieldSource, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButtonOk, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonTarget, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonSource, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldSource, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonSource))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonTarget)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jTextFieldTarget, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonOk)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSourceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSourceActionPerformed
        // TODO add your handling code here:
        executeChooserOntology(jTextFieldSource);
    }//GEN-LAST:event_jButtonSourceActionPerformed

    private void jButtonTargetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonTargetActionPerformed
        // TODO add your handling code here:
        executeChooserOntology(jTextFieldTarget);
    }//GEN-LAST:event_jButtonTargetActionPerformed

    private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOkActionPerformed
        // TODO add your handling code here:
        try {
            matManager.initMatching(jTextFieldSource.getText(), jTextFieldTarget.getText());
            ontoManager.loadSource(matManager.srcOnto);
            ontoManager.loadTarget(matManager.targOnto);
            System.out.print(matManager.matching);
           
        } catch (OWLOntologyCreationException ex) {
            Logger.getLogger(ChooseOntoFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jButtonOkActionPerformed

    private void executeChooserOntology(JTextField textField){
        // TODO add your handling code here:
        JFileChooser jFileChooser = new JFileChooser();
        int result = jFileChooser.showOpenDialog(jFileChooser.getParent());
        File file;
        String endPath;
        switch (result) {
            case JFileChooser.APPROVE_OPTION:
                file = jFileChooser.getSelectedFile();
                String path = file.getAbsolutePath();
                endPath = "file:///";
                path = path.replace('\\','/');
                endPath = endPath.concat(path);
                endPath = endPath.replaceAll(" ", "%20");
                textField.setText(endPath);
                jFileChooser.getParent().remove(jFileChooser);
        }
    }


    /**
    * @param args the command line arguments
    */
    /*public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ChooseOntoFrame().setVisible(true);
            }
        });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonOk;
    private javax.swing.JButton jButtonSource;
    private javax.swing.JButton jButtonTarget;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextFieldSource;
    private javax.swing.JTextField jTextFieldTarget;
    // End of variables declaration//GEN-END:variables
    private static OntoViewManager ontoManager = OntoViewManager.getInstance();
    private static MatchingManager matManager = MatchingManager.getInstance();
}
